package com.example.androiddemo.day715;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.androiddemo.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MediaPlayerActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mVideoTitle, mTimeCurrent, mTimeTotal;
    private VideoView mVideoView;
    private ImageView mPrevious, mPauseOrStart, mNext;
    private SeekBar mSeekBar;
    private Handler mHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            // 顾名思义，下面的代码是在UI线程中执行的代码。
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.i("====================run", Integer.toString(mVideoView.getCurrentPosition()));
                    mSeekBar.setProgress(mVideoView.getCurrentPosition()); // seekbar的进度设置为视频进度
                    mTimeCurrent.setText(getFormatTime(mVideoView.getCurrentPosition())); // 时间显示为当前视频播放时间。

                }
            });
            mHandler.postDelayed(mRunnable, 16);
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player);
        initView();
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.video;
        // MeidaController的使用
        MediaController mediaController = new MediaController(this); // 注意导入的包是 android.widget.MediaController
        mVideoView.setMediaController(mediaController);
        mediaController.setMediaPlayer(mVideoView);
        mVideoView.setVideoURI(Uri.parse(uri));
        // 监听视频或音频资源是否准备完毕
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            // 如果视频或音频资源准备完毕，会执行onPrepared回调。
            @Override
            public void onPrepared(MediaPlayer mp) {
                mTimeCurrent.setText("00:00");    // 初始时间显示为00:00
                mTimeTotal.setText(getFormatTime(mVideoView.getDuration()));// 总时长为视频时长
                mVideoTitle.setText("Video");
                mSeekBar.setMax(mVideoView.getDuration());
            }
        });

         // 监听seekbar的变化
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            // seekbar进度改变时会执行该回调
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 如果是用户拖动进度条，执行该段代码
                if (fromUser) {
//                    mHandler.removeCallbacks(mRunnable);
                    mVideoView.seekTo(seekBar.getProgress()); // 视频跳转到拖动的进度
                    mTimeCurrent.setText(getFormatTime(seekBar.getProgress()));
                    Log.i("===============", Integer.toString(mVideoView.getCurrentPosition()));
                }

            }

            //  开始拖动进度条的瞬间会执行该方法
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
//                mHandler.removeCallbacks(mRunnable);
                mVideoView.pause();  // 停止播放视频
                mPauseOrStart.setImageResource(R.drawable.ic_action_start); // 图标换成播放
            }

            // 停止拖动进度条的瞬间会执行该方法
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
//                mVideoView.start();
                mVideoView.resume();
//                mHandler.postDelayed(mRunnable,16);
                Log.i("===============start", Integer.toString(mVideoView.getCurrentPosition()));
                mPauseOrStart.setImageResource(R.drawable.ic_action_pause); //  图标换成暂停
            }
        });
    }

    private void initView() {
        mVideoTitle = findViewById(R.id.video_title);
        mTimeCurrent = findViewById(R.id.time_current);
        mTimeTotal = findViewById(R.id.time_total);
        mVideoView = findViewById(R.id.videoView);
        mPrevious = findViewById(R.id.previous);
        mNext = findViewById(R.id.next);
        mPauseOrStart = findViewById(R.id.pause_or_start);
        mSeekBar = findViewById(R.id.video_seekbar);
        mNext.setOnClickListener(this);
        mPauseOrStart.setOnClickListener(this);
        mPrevious.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pause_or_start:
                if (!mVideoView.isPlaying() && mSeekBar.getProgress() == 0) {
                    mVideoView.start();
                    mPauseOrStart.setImageResource(R.drawable.ic_action_pause); // 播放图标换成暂停
                } else if (mVideoView.isPlaying()) {
                    mVideoView.pause();
                    mPauseOrStart.setImageResource(R.drawable.ic_action_start); // 播放图标换成暂停
                } else {
                    mVideoView.start();
                }
                mHandler.postDelayed(mRunnable, 16); // 没有这句的话，进度条就不会动
                break;
            default:
                break;
        }
    }

    // 时间格式化方法
    private String getFormatTime(int time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
        return dateFormat.format(new Date(time));
    }
}